import Router from 'koa-router'
import Hot from '../model/hotword.js'
import axios from '../tools/axios.js'

let router = new Router({prefix: '/search'})

/**
 * @dateTime:2018/01/17
 * @author:yang
 * @desc:搜索商品关键字
 */
router.get('/suggest' , async(ctx) => {
    let {keyword} = ctx.query;
    let {status , data} = await axios.get('https://nn.meituan.com/ptapi/suggest' , {
        params:{
            keyword    
        }
    })
    ctx.body = {
        code: 0,
        result: data
    }
})

/**
 * @dateTime:2018/02/01
 * @author:yang
 * @desc:推上首页热搜
 */
router.get('/hotword' , async(ctx) => {
    let {word,sort} = ctx.query;
    let result = await Hot.find({word});
    if(result.length){
        ctx.body = {
            code: 0,
            msg: '热搜词已经存在'
        }
        return
    }
    let hotWord = await Hot.create({word , sort});
    if(hotWord){
        ctx.body = {
            code: 0,
            msg: '数据录入成功',
        }
    }else{
        ctx.body = {
            code: -1,
            msg: '数据录入失败'
        }
    }
})

/**
 * @dateTime:2018/02/11
 * @author:yang
 * @desc:获取热搜词
 */
router.get('/hotSearchList' , async(ctx) => {
    let hotSearch = await Hot.find();
    ctx.body = {
        code: 0,
        data: hotSearch.map((item) => {
            return{
                word: item.word
            }
        })
    }
})

/**
 * @dateTime:2018/02/14
 * @author:yang
 * @desc:关键字搜索结果
 */
router.get('/searchResult' , async(ctx) => {
    let {offset,q} = ctx.query;
    let {status,data} = await axios.get('http://apimobile.meituan.com/group/v4/poi/pcsearch/99' , {
        params:{
            uuid: 'e0cf6e09ef1a4229be6e.1550050319.1.0.0',
            limit: 32,
            offset,
            q
        }
    })
    ctx.body = {
        code: 0,
        data
    }
})

export default router